#include "hal_temper.h"
#include "bsp_adc.h"
#include "bsp_usart.h"

uint8_t NtcIsError[NTC_MAX_NUM];
//tsHalTempCtrl haltempctrl;
//3863
// const uint16_t WATER_NTC_Tbl[RT_TABLE_NUM] =
// {
// 	3900,3857 ,3850 ,3843 ,3836 ,3828 ,3820 ,3812 ,3803 ,3794,
// 	3785,3775 ,3765 ,3755 ,3744 ,3733 ,3721 ,3709 ,3697 ,3684, 
// 	3671,3658 ,3644 ,3629 ,3614 ,3599 ,3583 ,3567 ,3550 ,3533,
// 	3516,3497 ,3479 ,3460 ,3440 ,3420 ,3400 ,3379 ,3357 ,3335,
// 	3313,3290 ,3266, 3242 ,3218 ,3193 ,3168 ,3142 ,3116 ,3090,
// 	3063,3035 ,3008 ,2979 ,2951 ,2922 ,2893 ,2864 ,2834 ,2804,
// 	2773,2742 ,2712 ,2680 ,2649 ,2617 ,2585 ,2553 ,2522 ,2489,
// 	2457,2424 ,2392 ,2360 ,2327 ,2294 ,2262 ,2229 ,2196 ,2164,
// 	2132,2099 ,2067 ,2035 ,2003 ,1971 ,1940 ,1908 ,1877 ,1846,
// 	1815,1785 ,1754 ,1724 ,1694 ,1665 ,1636 ,1607 ,1578 ,1550,
// 	1522,1493 ,1465 ,1437 ,1409 ,1381 ,1354 ,1328 ,1302 ,1276,
// 	1250,1225 ,1201 ,1177 ,1153 ,1129 ,1106 ,1084, 1061 ,1040,
//  1018,997  ,976  ,956  , 936 ,917  ,897  ,878  ,860  ,738 , 
// 	824, 807  ,790  ,773  , 757 ,741  ,725  ,710  ,695  ,680 , 
// 	666, 
// };
const uint16_t WATER_NTC_Tbl_PURE[RT_TABLE_NUM] =     //纯水温度
{
	/* 0   1	2	 	3	  4    5	6	 	7	  8    9  */
	3004, 2968, 2933, 2897, 2860, 2823, 2786, 2748, 2711, 2672, 
	/*10   11	12	 	13   14   15	16	 	17   18   19 */
 	2634, 2595, 2556, 2517, 2478, 2439, 2400, 2360, 2321, 2282, 
	/*20   21	22	 	23   24   25	26	 	27   28   29 */
 	2243, 2203, 2164, 2125, 2087, 2048, 2010, 1972, 1934, 1896, 
	/*30   31	32	 	33   34   35	36	 	37   38   39 */
 	1859, 1822, 1786, 1749, 1713, 1678, 1643, 1609, 1574, 1541, 
	/*40   41	42	 	43   44   45	46	 	47   48   49 */
 	1508, 1475, 1443, 1411, 1380, 1349, 1319, 1289, 1260, 1232, 
	/*50   51	52	 	53   54   55	56		57   58   59 */
 	1204, 1176, 1149, 1122, 1096, 1071, 1046, 1021, 997, 974, 
	/*60   61	62	 	63   64   65	66	 	67   68   69 */
	951,  928,  906,  885,   864, 843,  823,  804,  784, 766, 
	/*70   71	72	 	73   74   75	76	 	77   78   79 */
 	747,  730,  712,  695,   678, 662,  646,  631,  616, 601, 
	/*80   81	82	 	83   84   85	86	 	87   88   89 */
 	587,  573,  559,  546,   533, 520,  508,  496,  484, 473, 
	/*90   91	92	 	93   94   95	96	 	97   98   99 */
 	462,  451,  440,  430,   420, 410,  401,  391,  382, 373, 
	/*100  101  102	 	103  104  105	106	 	107  108  109 */
 	365,  356,  348,  340,   332, 325,  317,  310,  303, 296, 
	/*110  111	112	 	113  114  115	116	 	117  118  119 120 */
	290,  283,  277,  270,   265, 259,  253,  247,  242, 237, 231,
};
//偏正
const uint16_t WATER_NTC_Tbl[RT_TABLE_NUM] =
{
	3976 ,	3970 ,	3964 ,	3957 ,	3950 ,	3943 ,	3936 ,	3928 ,	3920 ,	3912 ,	//0
	3903 ,	3894 ,	3885 ,	3875 ,	3865 ,	3855 ,	3844 ,	3832 ,	3821 ,	3808 ,	//10
	3796 ,	3783 ,	3769 ,	3756 ,	3741 ,	3726 ,	3712 ,	3696 ,	3680 ,	3664 ,	//20
	3647 ,	3630 ,	3612 ,	3594 ,	3575 ,	3556 ,	3537 ,	3517 ,	3496 ,	3475 ,	//30
	3454 ,	3432 ,	3409 ,	3386 ,	3363 ,	3339 ,	3315 ,	3290 ,	3265 ,	3239 ,	//40
	3213 ,	3187 ,	3160 ,	3133 ,	3105 ,	3077 ,	3049 ,	3020 ,	2991 ,	2962 ,	//50	
	2932 ,	2902 ,	2872 ,	2841 ,	2810 ,	2779 ,	2748 ,	2716 ,	2684 ,	2652 ,	//60
	2620 ,	2588 ,	2556 ,	2523 ,	2491 ,	2458 ,	2425 ,	2392 ,	2359 ,	2327 ,	//70
	2294 ,	2261 ,	2228 ,	2195 ,	2162 ,	2130 ,	2097 ,	2064 ,	2031 ,	1999 ,	//80
	1966 ,	1934 ,	1902 ,	1871 ,	1839 ,	1808 ,	1777 ,	1747 ,	1717 ,	1687 ,	//90	
	1657 ,	1628 ,	1599 ,	1571 ,	1542 ,	1515 ,	1487 ,	1460 ,	1433 ,	1407 ,	//100	
	1381 ,	1355 ,	1330 ,	1305 ,	1281 ,	1256 ,	1233 ,	1209 ,	1186 ,	1163 ,	//110	
	1141 ,	1119 ,	1097 ,	1076 ,	1055 ,	1034 ,	1013 ,	992  ,	971  ,	951  ,	//120
	931  ,	911  ,	892  ,	873  ,	855  ,	837  ,	819  ,	802  ,	785  ,	769  ,	//130
	752  ,																			//140
};

//偏负
//const uint16_t WATER_NTC_Tbl[RT_TABLE_NUM] =
//{
//	3863,3857 ,3850 ,3843 ,3836 ,3828 ,3820 ,3812 ,3803 ,3794, //0~9
//	3785,3775 ,3765 ,3755 ,3744 ,3733 ,3721 ,3709 ,3697 ,3684, 
//	3671,3658 ,3644 ,3629 ,3614 ,3599 ,3583 ,3567 ,3550 ,3533,
//	3516,3497 ,3479 ,3460 ,3440 ,3420 ,3400 ,3379 ,3357 ,3335,
//	3313,3290 ,3266, 3242 ,3218 ,3193 ,3168 ,3142 ,3116 ,3090,
//	3063,3035 ,3008 ,2979 ,2951 ,2922 ,2893 ,2864 ,2834 ,2804,
//	2773,2742 ,2712 ,2680 ,2649 ,2617 ,2585 ,2553 ,2522 ,2489,
//	2457,2424 ,2392 ,2360 ,2327 ,2294 ,2262 ,2229 ,2196 ,2164,
//	2132,2099 ,2067 ,2035 ,2003 ,1971 ,1940 ,1908 ,1877 ,1846,
//	1815,1785 ,1754 ,1724 ,1694 ,1665 ,1636 ,1607 ,1578 ,1550,
//	1522,1493 ,1465 ,1437 ,1409 ,1381 ,1354 ,1328 ,1302 ,1276,
//	1250,1225 ,1201 ,1177 ,1153 ,1129 ,1106 ,1084, 1061 ,1040,
//  1018,997  ,976  ,956  , 936 ,917  ,897  ,878  ,860  ,738 , 
//	824, 807  ,790  ,773  , 757 ,741  ,725  ,710  ,695  ,680 , 
//	666, 
//};

//陈冲测试
//const uint16_t WATER_NTC_Tbl[RT_TABLE_NUM] =
//{
//	3940 ,3930 ,3921 ,3911 ,3900 ,3889 ,3877 ,3865 ,3853 ,3839 ,//-40℃
//	3825 ,3811 ,3796 ,3780 ,3764 ,3747 ,3729 ,3711 ,3692 ,3672 ,
//	3651 ,3630 ,3608 ,3585 ,3562 ,3537 ,3512 ,3486 ,3460 ,3432 ,
//	3404 ,3375 ,3346 ,3315 ,3284 ,3252 ,3219 ,3186 ,3152 ,3117 ,
//	3082 ,3046 ,3009 ,2970 ,2932 ,2893 ,2853 ,2813 ,2773 ,2732 ,
//	2691 ,2650 ,2608 ,2566 ,2524 ,2482 ,2439 ,2397 ,2354 ,2312 ,
//	2269 ,2227 ,2184 ,2142 ,2100 ,2058 ,2017 ,1977 ,1937 ,1897 ,
//	1857 ,1818 ,1779 ,1741 ,1703 ,1666 ,1629 ,1592 ,1556 ,1521 ,
//	1486 ,1451 ,1418 ,1384 ,1352 ,1319 ,1293 ,1262 ,1231 ,1201 ,
//	1171 ,1143 ,1115 ,1087 ,1060 ,1033 ,1008 ,982  ,958  ,934  ,
//	910  ,887  ,865  ,843  ,821  ,800  ,780  ,760  ,741  ,722  ,
//	704  ,686  ,668  ,651  ,635  ,619  ,603  ,588  ,573  ,558  ,
//	544  ,530  ,517  ,504  ,491  ,479  ,467  ,455  ,444  ,433  ,
//	423  ,412  ,402  ,392  ,383  ,373  ,364  ,356  ,347  ,339  ,
//	330  ,																		//100℃
//};

// const uint16_t WATER_NTC_Tbl[RT_TABLE_NUM] =
// {
//     3928 ,	3921 ,	3914 ,	3907 ,	3899 ,	3891 ,	3883 ,	3875 ,	3866 ,	3857 ,
// 	3848 ,	3838 ,	3828 ,	3817 ,	3806 ,	3795 ,	3783 ,	3771 ,	3759 ,	3746 ,	
// 	3732 ,	3718 ,	3704 ,	3690 ,	3674 ,	3659 ,	3643 ,	3626 ,	3609 ,	3592 ,	
// 	3574 ,	3555 ,	3537 ,	3517 ,	3497 ,	3477 ,	3456 ,	3435 ,	3413 ,	3390 ,	
// 	3368 ,	3344 ,	3321 ,	3296 ,	3272 ,	3246 ,	3221 ,	3195 ,	3168 ,	3141 ,	
// 	3114 ,	3086 ,	3058 ,	3029 ,	3000 ,	2971 ,	2941 ,	2911 ,	2881 ,	2850 ,	
// 	2819 ,	2788 ,	2757 ,	2725 ,	2693 ,	2661 ,	2628 ,	2596 ,	2563 ,	2531 ,	
// 	2498 ,	2465 ,	2431 ,	2399 ,	2365 ,	2332 ,	2299 ,	2266 ,	2233 ,	2200 ,	
// 	2167 ,	2134 ,	2101 ,	2069 ,	2036 ,	2004 ,	1972 ,	1940 ,	1908 ,	1877 ,	
// 	1845 ,	1814 ,	1783 ,	1753 ,	1723 ,	1693 ,	1663 ,	1633 ,	1604 ,	1576 ,	
// 	1547 ,	1518 ,	1489 ,	1460 ,	1432 ,	1404 ,	1377 ,	1350 ,	1323 ,	1297 ,	
// 	1271 ,
// };
//-------------------------------------------------------------------------------
// void init_temper(void)
// {
// 	haltempctrl.temp_water = 0;
// 	haltempctrl.temp_heater = 0;
// }

//-------------------------------------------------------------------------------
typedef struct
{
	teADCDMANum ADCNum;     //温度索引 在DMA处处理

}tsNTCCtrl;

const tsNTCCtrl ntcCtrlTable[NTC_MAX_NUM]=
{		//ADC对应的枚举   				//正向引脚               //正向端口号                   /反向引脚             //反向端口号
		{ADC_NTC_HEAT},
		{ADC_NTC_PURE},
		{ADC_NTC_WATER_OUT},

};

uint16_t ad_convert_to_temper(uint16_t advalue,teNTCIndex NTCindex)  //@7 查表确定
{
	uint16_t i;
	uint16_t tmp0, tmp1;
	
	uint16_t ntc_table_num;
	const uint16_t *ntc_RT_tbl;
	if(NTCindex == NTC_PURE)
	{
			ntc_RT_tbl = WATER_NTC_Tbl_PURE;//纯水NTC表格
	}
	else
	{
			ntc_RT_tbl = WATER_NTC_Tbl; //其他NTC的表格
	}
	ntc_table_num = RT_TABLE_NUM;

	if( advalue>= ntc_RT_tbl[0] )
	{
		return NTC_OPEN;
	}
	else if( advalue <= ntc_RT_tbl[ntc_table_num-1] )
	{
		return NTC_SHORT;
		
	}

	for(i=0; i<ntc_table_num-1; ++i)
	{
		if( (ntc_RT_tbl[i] >= advalue) && (ntc_RT_tbl[i+1] < advalue) )  //@7判断区间
		{
			tmp0 = ntc_RT_tbl[i] - advalue; //3004 -3000
			tmp1 = ntc_RT_tbl[i] - ntc_RT_tbl[i+1];//3004-2968=36
			tmp0 = tmp0*10/tmp1; //@7  在这个里面 占的百分比 然后扩大10倍
			i = i*10 + tmp0; 
			goto EX_TEMPER;
		}
	}

EX_TEMPER:
	return i;
}

//-------------------------------------------------------------------------------
float OnReadNTCtemper(teNTCIndex NTCindex)//@7 缩小10倍的算法
{
	uint16_t data_temper = 0;
    float data_temper_f;
	data_temper = ad_convert_to_temper(adc_read_ch_filter(NTCindex),NTCindex);
	if( (data_temper == NTC_OPEN) || (data_temper == NTC_SHORT) )
	{
		NtcIsError[NTCindex] =1;  //故障
		return data_temper;
		  
	}
  data_temper_f = data_temper/10.0;
  NtcIsError[NTCindex] =0;  //故障解除
  return data_temper_f;

}

uint32_t OnGetNTCError(teNTCIndex NTCindex) //获取TDS故障
{
	return NtcIsError[NTCindex];
}

//-------------------------------------------------------------------------------
// 10 x temper
uint16_t get_ntc_temper_ex(teNTCIndex NTCindex)
{
	uint16_t data_temper = 0;

	data_temper = ad_convert_to_temper(adc_read_ch_filter(NTCindex),NTCindex);

	if( (data_temper == NTC_OPEN) || (data_temper == NTC_SHORT) )
	{
		return data_temper;
	}

//	if( adcindex == ADC_WATER_CUR )
//		haltempctrl.temp_water = data_temper;
//	else if( adcindex == ADC_PIPE_CUR )
//		haltempctrl.temp_heater = data_temper;
    
	return data_temper;
}

//-------------------------------------------------------------------------------
void find_temper_fifoch_min_max(ts_TemperFifoCtrl *pfifo)
{
	int i = 0;

	pfifo->max = pfifo->buf[0];
	pfifo->min = pfifo->buf[0];
	
	for(i = 1; i < TEMPER_FIFO_LEN; i++)
	{
		pfifo->max = MAX(pfifo->max, pfifo->buf[i]);
		pfifo->min = MIN(pfifo->min, pfifo->buf[i]);
	}
}

//-------------------------------------------------------------------------------
// First In First Out
//-------------------------------------------------------------------------------
uint16_t temper_fifoch_push(ts_TemperFifoCtrl *pfifo, uint16_t data)   //@7 推一个与之前的平均
{
	if( pfifo->len >= TEMPER_FIFO_LEN )
	{
		pfifo->sum -= pfifo->buf[pfifo->bottom];
		pfifo->bottom++;
		pfifo->bottom %= TEMPER_FIFO_LEN;// 保证bottom范围在[0,(FIFO_LEN-1)]
		
		pfifo->buf[pfifo->top++] = data;
		pfifo->top %= TEMPER_FIFO_LEN;// 保证top范围在[0,(FIFO_LEN-1)]
		pfifo->sum += data;

		find_temper_fifoch_min_max(pfifo);
		//去掉最大值、最小值     ， +1 四舍五入
		pfifo->average = (pfifo->sum + pfifo->average - pfifo->min - pfifo->max + 1 ) / (TEMPER_FIFO_LEN - 1);
		pfifo->len = TEMPER_FIFO_LEN;
	}
	else
	{
		pfifo->buf[pfifo->top++] = data;
		pfifo->top %= TEMPER_FIFO_LEN;
		pfifo->len++;
		pfifo->sum += data;
		pfifo->average = pfifo->sum / pfifo->len;
	}
	return pfifo->average;
}

//-------------------------------------------------------------------------------
void temper_fifoch_clear(ts_TemperFifoCtrl *pfifo)
{
	pfifo->sum = 0;
	pfifo->average = 0;
	pfifo->len = 0;
	pfifo->top = 0;
	pfifo->bottom = 0;
}



//-------------------------------------------------------------------------------

uint32_t adc_read_ch(teNTCIndex NTCindex)
{
	 return OnReadADCDMAvalue(ntcCtrlTable[NTCindex].ADCNum);
}

//-------------------------------------------------------------------------------
// ???????AD?
uint16_t adc_read_ch_filter(teNTCIndex NTCindex)
{
//	uint32_t sum_val = 0;
	uint32_t ave_val = 0;
//	uint32_t adc_max, adc_min;
//	uint32_t adc_value[ADC_FIL_TIMES] = {0};
//	uint8_t i;
	
	// for(i=0; i<ADC_FIL_TIMES; ++i)//?n?
	// {
	// 	adc_value[i] = adc_read_ch(adcindex);
	// }
   
	// adc_max = adc_value[0];
	// adc_min = adc_value[0];

	// //??
	// for(i=0; i<ADC_FIL_TIMES; ++i)
	// {
	// 	if( adc_value[i] >  adc_max )
	// 	{
	// 		adc_max = adc_value[i];
	// 	}

	// 	if( adc_value[i] <  adc_min )
	// 	{
	// 		adc_min = adc_value[i];
	// 	}
	// 	sum_val += adc_value[i];
	// }
	// printf("%d,%d\n",adc_min,adc_max);
	// ave_val = (sum_val - adc_max - adc_min) / (ADC_FIL_TIMES - 2);
	ave_val = adc_read_ch(NTCindex);
	return ave_val;
}

void OnTemperTest(void)
{
	  print_debug("NTC_HEAT: %f \r\n",OnReadNTCtemper(NTC_HEAT));//热水泵
	  print_debug("NTC_WATER_OUT: %f \r\n",OnReadNTCtemper(NTC_WATER_OUT));//热水泵
//	uint16_t buf[NTC_MAX_NUM];
//	for(uint8_t i=0;i<NTC_MAX_NUM; i++)
//	{
//		buf[i]=OnReadNTCtemper(NTC_HEAT);
//		switch(i)
//		{
//			case NTC_HEAT:		  print_debug("NTC_HEAT: %f \r\n",OnReadNTCtemper(NTC_HEAT));//热水泵
//			break;
//			case NTC_PURE: 			print_debug("NTC_PURE: %f \r\n",OnReadNTCtemper(NTC_PURE));//热水泵
//			break;
//			case NTC_WATER_OUT: print_debug("NTC_WATER_OUT: %f \r\n",OnReadNTCtemper(NTC_WATER_OUT));//热水泵
//			break;
//			default:
//			break;
//		}
//		
//	
//	}

}
